package admin

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"encoding/json"
	"fmt"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"strings"
)

type HighSchoolClass struct {
	Ctx iris.Context
}

type HighSchoolClassCreate struct {
	Id           string `json:"id"`
	HighSchoolId int64  `json:"highSchoolId"`
	Name         string `json:"name"`
}

type HighSchoolClassListParam struct {
	HighSchoolId int64 `json:"highSchoolId"`
	CurPage      int   `json:"curPage"`
	PerPage      int   `json:"perPage"`
}

func (c *HighSchoolClass) OptionsCreate() mvc.Result {
	return mvc.Response{
		Code: 204,
	}
}

func (c *HighSchoolClass) PostCreate() interface{} {
	highSchoolClassCreate := HighSchoolClassCreate{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析参数出错：%s", err.Error()), "")
	}
	err = json.Unmarshal([]byte(content), &highSchoolClassCreate)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析参数出错:%s", err.Error()), "")
	}

	if highSchoolClassCreate.HighSchoolId <= 0 {
		return utils.CommonRes(1000, "请选择高中", "")
	}
	if len(highSchoolClassCreate.Name) <= 0 {
		return utils.CommonRes(1000, "请输入班级名称", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	highSchoolClass := new(models.HighSchoolClass)
	highSchoolClass.HighSchoolId = highSchoolClassCreate.HighSchoolId
	highSchoolClass.Name = highSchoolClassCreate.Name
	_, err = db.InsertOne(highSchoolClass)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("操作失败%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "操作成功", "")
}

func (c *HighSchoolClass) OptionsList() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *HighSchoolClass) PostList() interface{} {
	highSchoolClassListParam := HighSchoolClassListParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接受请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &highSchoolClassListParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接受参数出错:%s", err.Error()), "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	start := (highSchoolClassListParam.CurPage - 1) * highSchoolClassListParam.PerPage
	highSchoolClassList := make([]models.HighSchoolClassObj, 0)
	sess := db.Table(new(models.HighSchoolClass)).Alias("hsc").
		Join("INNER", []string{"high_school", "hs"}, "hsc.high_school_id = hs.id").
		Limit(highSchoolClassListParam.PerPage, start)
	if highSchoolClassListParam.HighSchoolId > 0 {
		sess = sess.Where("hsc.high_school_id = ?", highSchoolClassListParam.HighSchoolId)
	}
	err = sess.Find(&highSchoolClassList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("获取高中班级列表失败：%s", err.Error()), "")
	}

	countSess := db.Where("id > ?", 0)
	if highSchoolClassListParam.HighSchoolId > 0 {
		countSess = countSess.Where("high_school_id = ?", highSchoolClassListParam.HighSchoolId)
	}
	total, err := countSess.Count(new(models.HighSchoolClass))
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("获取高中班级列表失败:%s", err.Error()), "")
	}

	data := map[string]interface{}{
		"total": total,
		"list":  highSchoolClassList,
	}

	return utils.CommonRes(20000, "成功获取高中班级列表", data)
}

func (c *HighSchoolClass) OptionsEdit() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *HighSchoolClass) PostEdit() interface{} {
	highSchoolClassCreate := HighSchoolClassCreate{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &highSchoolClassCreate)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	if highSchoolClassCreate.HighSchoolId <= 0 {
		return utils.CommonRes(1000, "请选择高中", "")
	}
	if len(highSchoolClassCreate.Name) <= 0 {
		return utils.CommonRes(1000, "请输入班级名称", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	updateData := map[string]interface{}{
		"high_school_id":    highSchoolClassCreate.HighSchoolId,
		"name": highSchoolClassCreate.Name,
	}

	_, err = db.Table(new(models.HighSchoolClass)).ID(highSchoolClassCreate.Id).Update(updateData)
	if err != nil {
		return utils.CommonRes(1000, "操作失败", "")
	}

	return utils.CommonRes(20000, "操作成功", "")
}

func (c *HighSchoolClass) OptionsDetail() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *HighSchoolClass) PostDetail() interface{} {
	editParam := utils.EditParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &editParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	if len(editParam.Id) <= 0 {
		return utils.CommonRes(1000, "请提供高中班级id", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	highSchoolClass := new(models.HighSchoolClass)
	ok, err := db.ID(editParam.Id).Get(highSchoolClass)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("信息获取失败:%s", err.Error()), "")
	}
	if !ok {
		return utils.CommonRes(1000, "信息获取失败", "")
	}

	return utils.CommonRes(20000, "信息获取成功", highSchoolClass)
}

func (c *HighSchoolClass) OptionsDel() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *HighSchoolClass) PostDel() interface{} {
	delParam := utils.DelParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &delParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	if len(delParam.Ids) <= 0 {
		return utils.CommonRes(1000, "请提供要删除项的id", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	ids := strings.Split(delParam.Ids, ",")
	for _, id := range ids {
		highSchoolClass := new(models.HighSchoolClass)
		db.ID(id).Get(highSchoolClass)
		db.ID(id).Delete(highSchoolClass)
	}

	return utils.CommonRes(20000, "操作成功", "")
}
